using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GUIPanelPerks : GUIPanelWithButtons {

	public int startingIndex;
	public MyGUIText perkPoints;
	public Material materialEnabled;
	public Material materialDisabled;
	
	private GameObject[] perksGUIList;
	private MyGUIText[] perksText;
	private List<Perks.Perk> allPerks;
	private int totalPerksCount;
	private List<Perks.Perk> ePerks;
	
	private Ray mousePosition;
	private RaycastHit hitInfo;
	
	private Camera GUICamera;
	
	private string perkTooltip;
	
	private bool editMode;
	void Start(){
		totalPerksCount = Perks.Perk.GetPerksDictionary().Count;
		//editMode = false;
		perkTooltip = null;
		GUICamera = this.transform.parent.GetComponent<Camera>();
		allPerks = new List<Perks.Perk>();
		perksGUIList = new GameObject[buttons.Count];
		perksText = new MyGUIText[buttons.Count];
		//De todos los botones me guardo los GO qe representan a cada PerkGUI
		for (int i=0; i<buttons.Count; i++){
			perksGUIList[i] = buttons[i].transform.parent.gameObject;
			perksText[i] = perksGUIList[i].GetComponentInChildren<MyGUIText>();
		}
		//Me guardo el diccionario con todos los perks existentes.
		Dictionary<string,Perks.Perk> perksDictionary = 
			Perks.Perk.GetPerksDictionary();
		//Los paso a una Lista mia para recorrerlos por indice.
		foreach(KeyValuePair<string, Perks.Perk> perkPair in perksDictionary){
			allPerks.Add(perkPair.Value);
		}
		startingIndex = 0;
		//ShowPerks();
		//UpdatePoints();
	}
	void Update(){
		mousePosition = GUICamera.ScreenPointToRay(Input.mousePosition);	
		
		if (Physics.Raycast(mousePosition, out hitInfo)){
			MyGUIText perkText = hitInfo.collider.GetComponent<MyGUIText>();
			if (perkText != null){
				Perks.Perk perk = Perks.Perk.GetPerk(perkText.Text);
                if (perk != null)
                {
                    MouseController.instance.drawCursor = false;
                    perkTooltip = perk.GetDescription() +
                        "\nPerk Points: " + perk.GetCost() +
                        perk.GetRequirementsDescription();
                }
						
			}else{
                MouseController.instance.drawCursor = true;
				perkTooltip = null;
			}	
		} 
		if (Input.GetAxis("Mouse ScrollWheel") > 0){
			ScrollPerk(true);
		}
		if (Input.GetAxis("Mouse ScrollWheel") < 0){
			ScrollPerk(false);
		}
				
		ShowPerks();
		UpdatePoints();
	}
	public void UpdatePoints(){
		this.perkPoints.SetText("                        "); 
		this.perkPoints.SetText("Perk Points: " + 
			CharacterDataManager.GetInstance().GetCurrentCharData().perks.GetAvailablePoints() + "/" +
			CharacterDataManager.GetInstance().GetCurrentCharData().perks.GetCapacityPoints() );
	}
	
	//Muestra 10 perks según el indice actual, es como un Slide.
	public void ShowPerks(){
		if (editMode){
			
			//Recorro 10 perks porque es la cantidad q entra en la ventana GUI.
			int j=startingIndex;
			for (int i=0; i<8; i++,j++){
				if (i<allPerks.Count){
					perksText[i].SetText("                     ");
					perksText[i].SetText(allPerks[j].GetName() );
					VerifyPerkActivation(perksText[i]);
				}
			}
		}else{ //Solo muestro los perks equipados.
			int j=startingIndex;
			ePerks = CharacterDataManager.GetInstance()
			.GetCurrentCharData().perks.GetEquippedPerks();
			
			for (int i=0; i<8; i++,j++){
				if (perksText[i] == null)
					break;
				perksText[i].SetText("                     ");
				if (j<ePerks.Count){
					perksText[i].SetText(ePerks[j].GetName() );
				} 
			}
		}
	}
	private void VerifyPerkActivation(MyGUIText perkGUIText){
		if (!editMode)
			return;
		if (CharacterDataManager.GetInstance().GetCurrentCharData()
		.perks.IsEquipped(perkGUIText.Text) ){
			perkGUIText.transform.parent.GetComponentInChildren<GUIButton>()
			.renderer.material = materialEnabled;
		} else{
			perkGUIText.transform.parent.GetComponentInChildren<GUIButton>()
			.renderer.material = materialDisabled;	
		}
	}
	void OnGUI(){
		if (perkTooltip != null){
			GUI.TextField(new Rect(
				Input.mousePosition.x + 10,//Screen.width/2 - textFieldWidth/2, 
				(Screen.height - Input.mousePosition.y) + 10,//Screen.height/2 - textFieldHeight/2,
				400,//textFieldWidth, 
				100),//textFieldHeight), 
				perkTooltip);
		}
	}
	public void ScrollPerk(bool scrollUp){
		if (scrollUp){
			if (startingIndex > 0)
				startingIndex--;
		}else if (editMode){
			if (startingIndex < totalPerksCount-10){
				startingIndex++;	
			}
		}else{
			if (startingIndex < totalPerksCount-10 && ePerks.Count>10){
				startingIndex++;	
			}
		}
	}
	public void EnableEditMode(bool active){
		Debug.Log("edit: " + active);
		if (active)
			startingIndex = 0;
		this.editMode = active;
		foreach (GUIButton button in buttons){
			button.gameObject.SetActive(active);	
			button.enabled = active;
		}
	}
}
